package com.shengyuan.manage_os.ui.fragment;

import android.annotation.TargetApi;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.base.BaseFragment;
//import com.shengyuan.oa.bean.Contacts;
import com.shengyuan.manage_os.im.common.base.CCPCustomViewPager;
import com.shengyuan.manage_os.im.common.base.CCPLauncherUITabView;
import com.shengyuan.manage_os.im.common.utils.LogUtil;
import com.shengyuan.manage_os.im.ui.ConversationListFragment;
import com.shengyuan.manage_os.im.ui.DiscussionListFragment;
import com.shengyuan.manage_os.im.ui.GroupListFragment;
import com.shengyuan.manage_os.im.ui.LauncherActivity;
import com.shengyuan.manage_os.im.ui.TabFragment;
import com.shengyuan.manage_os.im.ui.contact.MobileContactActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/11/15.
 */

public class MessageFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private MessageFragment M;
//    private List<Contacts> dataList = new ArrayList<>();

    private CCPLauncherUITabView mLauncherUITabView;
    /**
     * 三个TabView所对应的三个页面的适配器
     */
    private CCPCustomViewPager mCustomViewPager;

    /**
     * 沟通、联系人、群组适配器
     */
    public LauncherViewPagerAdapter mLauncherViewPagerAdapter;

    private boolean mTabViewInit = false;
    /**
     * 当前显示的TabView Fragment
     */
    private int mCurrentItemPosition = -1;


    @TargetApi(17)
    public MessageFragment(){
        super();
        init();
    }
    public  MessageFragment init(){
//        if(M == null){
            transaction = manager.beginTransaction();
            M = this;
            transaction.add(R.id.fragment_content,M,"Message").commit();
//        }
        return M;
    }
    static View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.main_tab,null);
        mTabViewInit = true;
        mCustomViewPager = (CCPCustomViewPager) view.findViewById(R.id.pager);
        mCustomViewPager.setOffscreenPageLimit(4);

        if (mLauncherUITabView != null) {
            mLauncherUITabView.setOnUITabViewClickListener(null);
            mLauncherUITabView.setVisibility(View.VISIBLE);
        }
        mLauncherUITabView = (CCPLauncherUITabView) view.findViewById(R.id.laucher_tab_top);
        mCustomViewPager.setSlideEnabled(true);
        mLauncherViewPagerAdapter = new LauncherViewPagerAdapter((FragmentActivity) getActivity(),
                mCustomViewPager);
        mLauncherUITabView
                .setOnUITabViewClickListener(mLauncherViewPagerAdapter);

        ctrlViewTab(0);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    public void ctrlViewTab(int index) {


        if ((!mTabViewInit || index < 0)
                || (mLauncherViewPagerAdapter != null && index > mLauncherViewPagerAdapter
                .getCount() - 1)) {
            return;
        }

        if (mCurrentItemPosition == index) {
            return;
        }
        mCurrentItemPosition = index;

        if (mLauncherUITabView != null) {
            mLauncherUITabView.doChangeTabViewDisplay(mCurrentItemPosition);
        }

        if (mCustomViewPager != null) {
            mCustomViewPager.setCurrentItem(mCurrentItemPosition, false);
        }

    }

    private class LauncherViewPagerAdapter extends FragmentStatePagerAdapter
            implements ViewPager.OnPageChangeListener,
            CCPLauncherUITabView.OnUITabViewClickListener {
        /**
         *
         */
        private int mClickTabCounts;
        private GroupListFragment mGroupListFragment;
        private DiscussionListFragment mDissListFragment;

        private final ViewPager mViewPager;
        private final ArrayList<LauncherViewPagerAdapter.TabInfo> mTabs = new ArrayList<>();

        final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        public LauncherViewPagerAdapter(FragmentActivity fm, ViewPager pager) {
            super(fm.getSupportFragmentManager());
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(String tabSpec, Class<?> clss, Bundle args) {
            String tag = tabSpec;

            LauncherViewPagerAdapter.TabInfo info = new LauncherViewPagerAdapter.TabInfo(tag, clss, args);
            mTabs.add(info);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return getTabView(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
                    "onPageScrollStateChanged state = " + state);

            if (state != ViewPager.SCROLL_STATE_IDLE
                    || mGroupListFragment == null) {
                return;
            }
            if (mGroupListFragment != null) {
                mGroupListFragment.onGroupFragmentVisible(true);
                mGroupListFragment = null;
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
                    "onPageScrolled " + position + " " + positionOffset + " "
                            + positionOffsetPixels);
            if (mLauncherUITabView != null) {
                mLauncherUITabView.doTranslateImageMatrix(position,
                        positionOffset);
            }
            if (positionOffset != 0.0F&&position==CCPLauncherUITabView.TAB_VIEW_THIRD) {
                if (mGroupListFragment == null) {
                    mGroupListFragment = (GroupListFragment) getTabView(CCPLauncherUITabView.TAB_VIEW_THIRD);
                    mGroupListFragment.onGroupFragmentVisible(true);
                }
            }
            if (positionOffset != 0.0F&&position==CCPLauncherUITabView.TAB_VIEW_FOUR) {
                if (mDissListFragment == null) {
                    mDissListFragment = (DiscussionListFragment) getTabView(CCPLauncherUITabView.TAB_VIEW_FOUR);
                    mDissListFragment.onDisGroupFragmentVisible(true);
                }
            }


        }

        @Override
        public void onPageSelected(int position) {
            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
                    "onPageSelected");
            if (mLauncherUITabView != null) {
                mLauncherUITabView.doChangeTabViewDisplay(position);
                mCurrentItemPosition = position;
            }
        }

        @Override
        public void onTabClick(int tabIndex) {
            if (tabIndex == mCurrentItemPosition) {
                LogUtil.d(
                        LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
                        "on click same index " + tabIndex);
                // Perform a rolling
                TabFragment item = (TabFragment) getItem(tabIndex);
//                item.onTabFragmentClick();
                System.out.println("@@@@@@@@@@@@@=!!!!!!!!!!!!===="+tabIndex);
                return;
            }

            mClickTabCounts += mClickTabCounts;
            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
                    "onUITabView Click count " + mClickTabCounts);
            mViewPager.setCurrentItem(tabIndex);
        }

    }

    private final HashMap<Integer, android.support.v4.app.Fragment> mTabViewCache = new HashMap<>();

    public final com.shengyuan.manage_os.im.ui.BaseFragment getTabView(int tabIndex) {
        LogUtil.d(LogUtil.getLogUtilsTag(LauncherActivity.class),
                "get tab index " + tabIndex);
        if (tabIndex < 0) {
            return null;
        }

        if (mTabViewCache.containsKey(Integer.valueOf(tabIndex))) {
            return (com.shengyuan.manage_os.im.ui.BaseFragment) mTabViewCache.get(Integer.valueOf(tabIndex));
        }

        com.shengyuan.manage_os.im.ui.BaseFragment mFragment = null;
        switch (tabIndex) {
            case 0:
                mFragment = (TabFragment) android.support.v4.app.Fragment.instantiate(getActivity(),
                        ConversationListFragment.class.getName(), null);
                break;
            case 1:
                mFragment = (TabFragment) android.support.v4.app.Fragment
                        .instantiate(getActivity(),
                                MobileContactActivity.MobileContactFragment.class
                                        .getName(), null);
                break;
            case 2:
                mFragment = (TabFragment) android.support.v4.app.Fragment.instantiate(getActivity(),
                        GroupListFragment.class.getName(), null);
                break;
            case 3:

                mFragment = (TabFragment) android.support.v4.app.Fragment.instantiate(getActivity(),
                        DiscussionListFragment.class.getName(), null);
                break;

            default:
                break;
        }

        if (mFragment != null) {
            mFragment.setActionBarActivity((FragmentActivity) getActivity());
        }
        mTabViewCache.put(Integer.valueOf(tabIndex), mFragment);
        return mFragment;
    }

}
